<template>
    <div class="order-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <company-select 
                    @select-company="handleSelectCompany"
                    custom-class="w-[180px]"
                    style="margin-right: 20px;"
                />
                <el-form-item label="开始时间" class="w-[260px]" style="margin-right: 20px;">
                    <el-date-picker
                        v-model="queryParams.start_time"
                        type="datetime"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="结束时间" class="w-[260px]" style="margin-right: 20px;">
                    <el-date-picker
                        v-model="queryParams.end_time"
                        type="datetime"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="状态">
                    <el-select v-model="queryParams.status" class="w-[120px]" clearable>
                        <el-option label="全部" value="" />
                        <el-option label="已下单" value="10" />
                        <el-option label="已匹配" value="20" />
                        <el-option label="已出发" value="30" />
                        <el-option label="已到达" value="40" />
                        <el-option label="已支付" value="50" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="getLists">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-table
                    ref="tableRef"
                    class="mt-4"
                    size="large"
                    v-loading="pager.loading"
                    :data="pager.lists"
                >
                    <el-table-column label="订单信息" min-width="250">
                        <template #default="{ row }">
                            <div>公司名称: {{ row.company.company_name }}</div>
                            <div>订单编号: {{ row.order_num }}</div>
                            <div>订单状态: {{ getOrderStatusText(row.status) }}</div>
                            <div>订单距离: {{ Math.ceil(row.extend.driver_mile / 1000) }} 公里</div>
                            <div>订单金额: {{ row.price / 100 }}元</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="司机信息" min-width="240">
                        <template #default="{ row }">
                            <div>司机姓名: {{ row.driver_name }}</div>
                            <div>司机姓名: {{ row.driver_phone }}</div>
                            <div>车牌号: {{ row.vehicle_no }}</div>
                            <div>驾驶证号: {{ row.driver_license }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="订单时间" min-width="200">
                        <template #default="{ row }">
                            <div>下单时间: {{ row.created_at > 0 ? timeFormat(row.created_at,"yyyy-mm-dd hh:MM") : '-' }}</div>
                            <div>用车时间: {{ row.book_time > 0 ? timeFormat(row.book_time,"yyyy-mm-dd hh:MM") : '-' }}</div>
                            <div>匹配时间: {{ row.distribute_time > 0 ? timeFormat(row.distribute_time,"yyyy-mm-dd hh:MM") : '-' }}</div>
                            <div>出发时间: {{ row.depart_time > 0 ? timeFormat(row.depart_time,"yyyy-mm-dd hh:MM") : '-' }}</div>
                            <div>到达时间: {{ row.arrive_time > 0 ? timeFormat(row.arrive_time,"yyyy-mm-dd hh:MM") : '-' }}</div>
                            <div>支付时间: {{ row.pay_time > 0 ? timeFormat(row.pay_time,"yyyy-mm-dd hh:MM") : '-' }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="地址信息" min-width="270">
                        <template #default="{ row }">
                            <div>出发地址: {{ row.departure }}</div>
                            <div>目的地址: {{ row.destination }}</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="80">
                        <template #default="{ row }">
                            <el-button
                                type="danger"
                                link
                                @click="getSuperviseLog(row)"
                            >
                                监管日志
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="order">
import type { ElTable, FormInstance } from 'element-plus'
import CompanySelect from '@/components/company-select/index.vue'
import { shallowRef, reactive } from 'vue'
import dayjs from 'dayjs'
import { orderLists } from '@/api/supervise/order'
import { timeFormat } from '@/utils/util'
import { getOrderStatusText} from '@/utils/supervise'
import { usePaging } from '@/hooks/usePaging'

const tableRef = shallowRef<InstanceType<typeof ElTable>>()
const formRef = shallowRef<FormInstance>()

const queryParams = reactive({
    company_id: '',
    status: '',
    start_time: dayjs().subtract(7, 'day').format('YYYY-MM-DD HH:mm:ss'),
    end_time: dayjs().format('YYYY-MM-DD HH:mm:ss'),
})

const getSuperviseLog = (row:any) => {
    // window.open(`/supervise/order-log?id=${row.id}`)
}

const handleSelectCompany = (companyId:any) => {
    queryParams.company_id = companyId
}

const { pager, getLists, resetParams } = usePaging({
    fetchFun: orderLists,
    params: queryParams,
})



onMounted(() => {
    getLists()
})
</script>